package io.leo.leoui.otherwidgets;

import android.content.Context;
import android.graphics.drawable.PaintDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import io.leo.leoui.R;
import io.leo.leoutils.SoftKeyboardUtils;
import io.leo.leoutils.DisplayUtils;

public class BottomInputWindow extends PopupWindow {
    private Context context;
    private View.OnClickListener listener;

    private EditText editText;
    private ImageButton submit;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            SoftKeyboardUtils.showKeyboard(editText);
        }
    };
    public BottomInputWindow(Context context,View rootView){
        super(LayoutInflater.from(context).inflate(R.layout.input_bar,null), LinearLayout.LayoutParams.MATCH_PARENT, DisplayUtils.dp2Px(context,40));
        init(context,rootView);
    }


    private void init(final Context context, View rootView) {
        this.context = context;
        setFocusable(true);
        setBackgroundDrawable(new PaintDrawable());
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        showAtLocation(rootView, Gravity.BOTTOM, 0, 0);

        editText = getContentView().findViewById(R.id.ib_tv);
        submit = getContentView().findViewById(R.id.ib_send);
        //延时弹起软键盘
        new Thread(){
            @Override
            public void run() {
                super.run();
                handler.sendMessageDelayed(handler.obtainMessage(),100);
            }
        }.start();
    }

    public void setSubmitClickListener(View.OnClickListener listener){
        submit.setOnClickListener(listener);
    }

    public EditText getEditText(){
        return editText;
    }


}
